package online.shenme.day01.StudentManager.dao;

import online.shenme.day01.StudentManager.domain.Person;
import online.shenme.day01.StudentManager.domain.Student;

import java.util.ArrayList;

public class StudentDao implements BaseDao {
    ArrayList<Student> list = new ArrayList<>();

    @Override
    public boolean reSet(Person newMan) {
        if (findIndex(newMan.getId()) >= 0) {
            list.set(findIndex(newMan.getId()), (Student) newMan);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean add(Person newMan) {
        if (findIndex(newMan.getId()) == -1) {
            list.add((Student) newMan);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean reNull(String id) {
        if (findIndex(id) >= 0) {
            list.remove(findIndex(id));
            return true;
        } else {
            return false;
        }
    }

    @Override
    public ArrayList<Student> all() {
        return list;
    }


    @Override
    public int findIndex(String id) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (id.equals((list.get(i)).getId())) {
                return i;
            }
        }
        return index;
    }
}
